package umd.mhealth.lab2;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		final Button buttonOne = (Button) findViewById(R.id.button_one);
		buttonOne.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				View view = (View) findViewById(R.id.main);
				view.setBackgroundColor(Color.RED);
			}
		});
		
		final Button buttonTwo = (Button) findViewById(R.id.button_two);
		buttonTwo.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				View view = (View) findViewById(R.id.main);
				view.setBackgroundColor(Color.BLUE);
			}
		});
		
		if (savedInstanceState != null){
			  super.onRestoreInstanceState(savedInstanceState);
			  int bgColor = savedInstanceState.getInt("bgColor");
			  View view = (View) findViewById(R.id.main);
			  view.setBackgroundColor(bgColor);
		  }
	}
	
	
	public void onPause(){
		super.onPause();
	}
	
	@Override
	public void onSaveInstanceState(Bundle savedInstanceState){
		super.onSaveInstanceState(savedInstanceState);
		View view = (View) findViewById(R.id.main);
		int bgColor = Color.TRANSPARENT;
		Drawable bg = (Drawable) view.getBackground();
		if (bg instanceof ColorDrawable)
            bgColor = ((ColorDrawable) bg).getColor();
		savedInstanceState.putInt("bgColor", bgColor);
	}

}
